package com.example.service.Impl;

import com.example.common.exception.BaseException;
import com.example.entity.dto.SalaryDto;
import com.example.entity.vo.WageVO;
import com.example.mapper.SalaryMapper;
import com.example.service.SalaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;
    /**
     * 计算薪资并保存
     * @param salaryDto
     */

    public void savesalary(SalaryDto salaryDto) {

        float workingtime=salaryDto.getWorkingTime();
        float overTime = salaryDto.getOverTime();
        float dayCosts = salaryDto.getDayCosts();
        float overtimeCosts = salaryDto.getOvertimeCosts();
        float advance = salaryDto.getAdvance();
        float kilometers = salaryDto.getKilometers();
        float monovalent = salaryDto.getMonovalent();
        float travelExpenses = salaryDto.getTravelExpenses();
        float regularPay = workingtime*dayCosts;
        float overtimePay = overTime * overtimeCosts;
        float travelPay = kilometers * monovalent;
        float totalPay = regularPay + overtimePay + travelPay + travelExpenses - advance;
        salaryMapper.savesalary(salaryDto.getEmployeeId(),totalPay);
    }
    public WageVO getsalary(int id, String date) {
        SalaryDto salaryDto =  salaryMapper.getsalary(id,date);
        WageVO wageVO = new WageVO();
        if(salaryDto == null){
            return wageVO;
        }
        BeanUtils.copyProperties(salaryDto,wageVO);
        float workingSalary = salaryDto.getWorkingTime() * salaryDto.getDayCosts();
        float overSalary = salaryDto.getOverTime() * salaryDto.getOvertimeCosts();
        wageVO.setWorkingSalary(workingSalary);
        wageVO.setOverSalary(overSalary);
        return wageVO;
    }

    /**
     * 保存员工基础信息
     * @param id
     * @param date
     */
    public void save(int id, String date) {
        salaryMapper.save(id,date);
    }

    /**
     * 保存员工数据
     * @param wageVO
     */
    public void update(WageVO wageVO) {
        if(wageVO.getEmployeeId() == null){
            throw new BaseException("员工ID不能为空");
        }
        salaryMapper.update(wageVO);
    }

}
